package com.yugao.fintech.draper.admin.controller;

import com.yugao.fintech.draper.admin.entity.SysMenu;
import com.yugao.fintech.draper.core.model.TreeNode;
import com.yugao.fintech.draper.admin.service.MenuService;
import com.yugao.fintech.draper.admin.param.MenuDTO;
import com.yugao.fintech.draper.core.exception.BaseException;
import com.yugao.fintech.draper.core.model.ResultVO;
import com.yugao.fintech.draper.security.annotation.SecurityInner;
import com.yugao.fintech.draper.security.SecurityUser;
import com.yugao.fintech.draper.security.util.SecurityUserUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

@Slf4j
@RestController
@RequestMapping("/menu")
@Tag(name = "系统菜单管理")
public class MenuController {
    @Autowired
    private MenuService menuService;

    @Operation(summary = "获取用户路由信息和用户权限信息")
    @GetMapping("/nav")
    public ResultVO<Map<String, Object>> getUserRoutersAndAuthority() {
        return ResultVO.success(menuService.getUserRoutersAndAuthority());
    }

    @Operation(summary = "获取用户角色下的所有树形菜单信息(包括按钮)")
    @GetMapping("/getUserMenuTree")
    public ResultVO<List<TreeNode<MenuDTO>>> getUserMenuTree() {
        SecurityUser authUser = Optional.ofNullable(SecurityUserUtil.getCurrentUser()).orElseThrow(() -> new BaseException("用户未登录！"));
        return ResultVO.success(menuService.getUserMenuTree(authUser.getUserId()));
    }

    @Operation(summary = "获取全部的树形菜单信息(包括按钮)")
    @GetMapping("/getAllMenuTree")
    public ResultVO<List<TreeNode<MenuDTO>>> getAllMenuTree() {
        return ResultVO.success(this.menuService.getAllMenuTree());
    }

    @Operation(summary = "获取菜单下拉框")
    @GetMapping("/getTreeSelect")
    public ResultVO<List<TreeNode<MenuDTO>>> getTreeSelect() {
        return ResultVO.success(this.menuService.getTreeSelect());
    }

    @Operation(summary = "保存路由信息")
    @PostMapping("/save")
    public void saveMenu(@RequestBody TreeNode treeNode) {
        this.menuService.saveMenu(treeNode);
    }

    @Operation(summary = "根据id删除路由信息")
    @DeleteMapping("/delete/{id}")
    public void deleteMenu(@PathVariable("id") Long id) {
        this.menuService.removeById(id);
    }

    @Operation(summary = "根据id查询路由信息")
    @GetMapping("/getMenuById/{id}")
    public ResultVO<TreeNode<SysMenu>> getMenuById(@PathVariable("id") Long id) {
        return ResultVO.success(this.menuService.getMenuById(id));
    }

    @Operation(summary = "更新路由")
    @PostMapping("/update")
    public void updateMenu(@RequestBody TreeNode treeNode) {
        this.menuService.updateMenu(treeNode);
    }

    @SecurityInner(inner = true)
    @Operation(summary = "通过用户名查询权限信息")
    @GetMapping("/findUserPermissions/{username}")
    public ResultVO<Set<String>> findUserPermissions(@PathVariable("username") String username) {
        return ResultVO.success(menuService.findUserPermissions(username));
    }
}
